﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using tenpay;

namespace NJL.Web.Payment.tenpay
{
    public partial class return_url : BasePageVisitor
    {
        NJL.BLL.NJL_BOOK_ORDER bllBookOrder = new BLL.NJL_BOOK_ORDER();
        NJL.Model.NJL_BOOK_ORDER model;
        protected NJL.BLL.NJL_SYS_CS_PAY_MAP bllPayMap = new BLL.NJL_SYS_CS_PAY_MAP();
        NJL.Model.NJL_SYS_CS_PAY_MAP modelPayMap;
        protected void Page_Load(object sender, EventArgs e)
        {
            modelPayMap = bllPayMap.GetModel("2");
            //密钥
            string key = modelPayMap.PAY_COMPANY_KEY;

            //创建PayResponseHandler实例
            PayResponseHandler resHandler = new PayResponseHandler(Context);

            resHandler.setKey(key);



            //判断签名
            if (resHandler.isTenpaySign())
            {
                //商户订单号
                string sp_billno = resHandler.getParameter("sp_billno");
                //交易单号
                string transaction_id = resHandler.getParameter("transaction_id");

                //金额金额,以分为单位
                string total_fee = resHandler.getParameter("total_fee");


                //支付结果
                string pay_result = resHandler.getParameter("pay_result");


                if ("0".Equals(pay_result))
                {

                    if (sp_billno.Contains("T")) //如果是临时订单
                    {
                        NJL.BLL.NJL_TEMP_ORDER blltempOrder = new NJL.BLL.NJL_TEMP_ORDER();
                        NJL.Model.NJL_TEMP_ORDER modeltempOrder = blltempOrder.GetModelbyOrderId(sp_billno);
                        modeltempOrder.ORDER_STATE = 2;
                        modeltempOrder.PAY_DATE_TIME = DateTime.Now;
                        blltempOrder.Update(modeltempOrder);
                        resHandler.doShow("/Shop/TempPaymentReturn.aspx?id=2&orderId=" + modeltempOrder.ID);
                    }
                    else
                    {
                        NJL.BLL.NJL_BOOK_ORDER bll = new NJL.BLL.NJL_BOOK_ORDER();
                        model = bll.GetModelByOrderNO(sp_billno);
                        model.PAY_MAP_TYPE_CODE = "2"; //更新支付方式
                        model.PAY_DATE_TIME = DateTime.Now;
                        model.ORDER_STATE = 2;
                        if (bll.Update(model))
                        {

                        }
                        //------------------------------
                        //处理业务开始
                        //------------------------------ 

                        //注意交易单不要重复处理
                        //注意判断返回金额

                        //------------------------------
                        //处理业务完毕
                        //------------------------------

                        //调用doShow, 打印meta值跟js代码,告诉财付通处理成功,并在用户浏览器显示$show页面.
                        NJL.BLL.NJL_BOOK bllBook = new NJL.BLL.NJL_BOOK();
                        NJL.Model.NJL_BOOK modelBook = bllBook.GetModel(model.NJL_BOOK_ID);
                        NJL.BLL.NJL_REG_PAY bllRegPay = new NJL.BLL.NJL_REG_PAY();
                        NJL.Model.NJL_REG_PAY modelRegPay;
                        modelRegPay = new NJL.Model.NJL_REG_PAY();
                        modelRegPay.ID = DateTime.Now.ToString("yyyyMMddHHmmssfff") + new Random().Next(100, 999).ToString();
                        modelRegPay.NJL_SYS_BASE_ID = WEBSITE_CODE;
                        modelRegPay.BOOK_ORDER_ID = model.ID;
                        modelRegPay.ORDER_NO = sp_billno;
                        modelRegPay.ORDER_TOTAL = Convert.ToDecimal(total_fee) / 100;
                        modelRegPay.ORDER_TYPE_CODE = modelBook.TYPE_CODE;
                        modelRegPay.PAY_MAP_TYPE_CODE = "2";        //2为财付通
                        modelRegPay.CREATE_DATE_TIME = DateTime.Now;
                        modelRegPay.CREATE_USER_ID = UserId;
                        modelRegPay.CREATE_USER_NAME = UserName;
                        if (bllRegPay.Add(modelRegPay))
                        {

                        }
                        if (modelBook.TYPE_CODE == "1")
                        {
                            resHandler.doShow("/AttractionsOrderOK.aspx?id=" + model.ID);
                        }
                        else if (modelBook.TYPE_CODE == "2")
                        {
                            if (model.IS_REGISTER_USER == 1)
                            {
                                resHandler.doShow("/CompanyOrderOK.aspx?id?id=" + model.ID);
                            }
                            else
                            {
                                resHandler.doShow("/CompanyOrderOK2.aspx?id?id=" + model.ID);
                            }
                        }
                    }

                }
                else
                {
                    //当做不成功处理
                    Response.Write("支付失败");
                }

            }
            else
            {
                Response.Write("认证签名失败");
                //string debugInfo = resHandler.getDebugInfo();
                //Response.Write("<br/>debugInfo:" + debugInfo);
            }
        }

    }
}